using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SlopeIntercept {

    public class Game1 : Game {
        
        public const int HORIZON_Y = 277; // Golden Ration horizon.
        public const int SCREEN_BOTTOM_Y = 720;
        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;
        private IList<Lane> Lanes = new List<Lane>();
        private Texture2D Reflector;
        private Texture2D Tree;
        private Texture2D Grass;

        public Game1() {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;
        }

        protected override void LoadContent() {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Reflector = Content.Load<Texture2D>("reflector");
            Tree = Content.Load<Texture2D>("tree");
            Grass = Content.Load<Texture2D>("grass");
            const int screenCenterX = 640;
            Point P1 = new Point(screenCenterX, HORIZON_Y); // All Lanes start at the horizon.
            for (int i = -1280; i < 2560; i += 64) {
                Lane lane = new Lane(P1, new Point(i, SCREEN_BOTTOM_Y));
                if (i == -640 || i == 640 || i == 1920) {
                    for(int j=HORIZON_Y; j<SCREEN_BOTTOM_Y; j+=20) {
                        lane.GameObjects.Add(new GameObject {Position = new Vector2(i, j), Texture = Reflector, Scale = 1, Speed = 10});
                    }
                }
                if (i == 0 || i == 1280) {
                    for(int j=HORIZON_Y; j<SCREEN_BOTTOM_Y; j+=75) {
                        lane.GameObjects.Add(new GameObject {Position = new Vector2(i, j), Texture = Tree, Scale = 1, Speed = 10});
                    }
                }
                if (i == -640 || i == 1920) {
                    for(int j=HORIZON_Y; j<SCREEN_BOTTOM_Y; j+=20) {
                        lane.GameObjects.Add(new GameObject {Position = new Vector2(i, j), Texture = Grass, Scale = 1, Speed = 10});
                    }
                }
                Lanes.Add(lane);
            }
        }

        protected override void Update(GameTime gameTime) {
            foreach (Lane lane in Lanes) {
                lane.Update(gameTime);
            }
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime) {
            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();
            foreach (Lane lane in Lanes) {
                lane.Draw(spriteBatch);
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }

    }

}